home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / gaplib.lha / GAPLib_Beta / examples / FuncOpt1.c next >
C/C++ Source or Header  |  1999-01-29  |  1KB  |  58 lines

  1. /*
  2.  * Minimal Example Function Optimization.
  3.  *
  4.  * Lots of improvements could be made such as adding a wee bit of
  5.  * elitism and using a mutator with higher mutational probability, but
  6.  * it really is only meant as an example of how short you can make
  7.  * this program with GAP-Lib. (Though adding less than one line of code
  8.  * will make it several times as good. :-)
  9.  *
  10.  * Fitness function: f(x) = x
  11.  *
  12.  * Population : 20 Individuals
  13.  *
  14.  */
  15.  
  16. #include <time.h>
  17. #include <stdio.h>
  18. #include <GAP.h>
  19.  
  20. /* The representation of the individuals. */
  21.  
  22. struct Polyphant {
  23.    unsigned char  v;
  24. };
  25.  
  26. double fitfunc(struct Polyphant *);
  27.  
  28. int main(void)
  29. {
  30. struct Population *Pop;
  31. int i;
  32.  
  33. EnterGAP(2); /* Initialize environment. */
  34. InitRand(time(NULL));   /* Initialize random number generator. */
  35.  
  36. Pop = CreatePopulation(20,sizeof(struct Polyphant),NULL); /* Create a population. */
  37.  
  38. if(Pop!=NULL) {
  39.    printf("Created %ld individuals.\n",Pop->NumPolys);
  40.    for(i=0;i!=25;i++) { /* Evolve 25 generations. */
  41.       Pop = EvolveT(Pop,EVL_Evaluator,fitfunc,TAG_DONE);
  42.       printf("Generation %ld: Best value = %.03f, Avg = %.03f, Median = %.03f\n",Pop->Generation,Pop->Stat.MaxFitness,Pop->Stat.AverageFitness,Pop->Stat.MedianFitness);
  43.    }
  44.  
  45.    printf("After 25 generations: Best individual = %ld\n",((struct Polyphant *)Pop->Stat.Max)->v);
  46.    DeletePopulation(Pop);
  47. } else {
  48.    fprintf(stderr,"Unable to create population.\n");
  49. }
  50.  
  51. return(0);
  52. }
  53.  
  54. double fitfunc(struct Polyphant *p)
  55. {
  56. return((double)p->v);
  57. }
  58.